﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace homework6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义两个选择文件的路径
        string wenjian1;
        string wenjian2;
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog f1 = new OpenFileDialog();
            f1.Title = "请选择文件";
            f1.Multiselect = false;//不可多选
            f1.Filter = "(*.txt) | *.txt";//过滤，只可选择文本文件
            if (f1.ShowDialog() == DialogResult.OK)
            {
                wenjian1 = f1.FileName;
                wen1.Text = f1.FileName;//在界面上展示文件路径
            }
        }

        private void file2_Click(object sender, EventArgs e)
        {
            OpenFileDialog f1 = new OpenFileDialog();
            f1.Title = "请选择文件";
            f1.Multiselect = false;
            f1.Filter = "(*.txt) | *.txt";
            if (f1.ShowDialog() == DialogResult.OK)
            {
                wenjian2 = f1.FileName;
                wen2.Text = f1.FileName;
            }
        }

        private void start_Click(object sender, EventArgs e)
        {   
            StreamReader sr1 = new StreamReader(wenjian1);
            StreamReader sr2 = new StreamReader(wenjian2);
            FileInfo fi = new FileInfo(@"D:\自己做的文档\charp\Data.txt");
           
            if (!fi.Exists)
            {
                //创建文件
                fi.Create().Close();
                exist.Text = "目的文件不存在，已新建";
            }
            else
            {
                exist.Text = "目的文件已存在";
            }
            //文件输出流
            StreamWriter sw = new StreamWriter(@"D:\自己做的文档\charp\Data.txt");  
            while (sr1.Peek()!=-1)//此处不能用ReadLine函数，测试时会导致文件合成不完整
            {
                string str = sr1.ReadLine();
                sw.WriteLine(str);
                sw.Flush();
            }
            while (sr2.Peek()!=-1)
            { 
                string str = sr2.ReadLine();
                sw.WriteLine(str);
            }
            file3.Text = "合成文件的路径为"+fi.FullName;
            sr1.Close();
            sr2.Close();
            sw.Close();
        }
    }
}
